//
//  YQWelComeViewController.m
//  WeiboOC
//
//  Created by yang on 15/11/15.
//  Copyright © 2015年 yang. All rights reserved.
//

#import "YQWelComeViewController.h"
@interface YQWelComeViewController ()
@property (nonatomic, strong) UIImageView* backImage;
@property (nonatomic, strong) UIImageView* iconImage;
@property (nonatomic, strong) UILabel* label;
@end
@implementation YQWelComeViewController
- (UIImageView*)backImage
{
    if (!_backImage) {
        UIImageView* backImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ad_background"]];
        _backImage = backImage;
    }
    return _backImage;
}
- (UIImageView*)iconImage
{
    if (!_iconImage) {
        UIImageView* iconImage = [[UIImageView alloc] init];
        _iconImage = iconImage;
    }
    return _iconImage;
}
- (UILabel*)label
{
    if (!_label) {
        YQUserAccountViewModel* user = [[YQUserAccountViewModel alloc] init];
        NSString* title = [NSString stringWithFormat:@"%@ 欢迎归来", user.userName];
        UILabel* label = [[UILabel alloc] initWithTitle:title size:19 color:[UIColor lightGrayColor] margin:0];
        label.alpha = 0;
        _label = label;
    }
    return _label;
}
- (void)loadView
{
    self.view = self.backImage;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupUI];
}
- (void)setupUI
{
    [self.view addSubview:self.iconImage];
    [self.view addSubview:self.label];
    [self.iconImage makeConstraints:^(MASConstraintMaker* make) {
        make.centerX.equalTo(self.view);
        make.centerY.equalTo(self.view.bottom).offset(-180);
        make.width.height.equalTo(90);
    }];
    [self.label makeConstraints:^(MASConstraintMaker* make) {
        make.top.equalTo(self.iconImage.bottom).offset(20);
        make.centerX.equalTo(self.view);
    }];
    YQUserAccountViewModel* user = [[YQUserAccountViewModel alloc] init];
    [self.iconImage sd_setImageWithURL:user.userHeadURL placeholderImage:[UIImage imageNamed:@"avatar_default_big"]];
    self.iconImage.layer.cornerRadius = 45;
    self.iconImage.layer.masksToBounds = YES;
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self startAnimation];
}
- (void)startAnimation
{
    [self.iconImage updateConstraints:^(MASConstraintMaker* make) {
        make.centerY.equalTo(self.view.bottom).offset(-screenH + 180);
    }];
    [UIView animateWithDuration:1.2
        delay:0
        usingSpringWithDamping:0.9
        initialSpringVelocity:9.8
        options:UIViewAnimationOptionTransitionNone
        animations:^{
            [self.view layoutIfNeeded];
        }
        completion:^(BOOL finished) {
            [UIView animateWithDuration:0.6
                animations:^{
                    self.label.alpha = 1;
                }
                completion:^(BOOL finished){
                    [[NSNotificationCenter defaultCenter] postNotificationName:AppSwitchRootViewControler object:nil];
                }];
        }];
}
@end
